﻿using System.Globalization;
using Telerik.Windows;
using Telerik.Windows.Controls;
using System.Windows.Controls;
using Telerik.Windows.Controls.Scheduler;

namespace MedLink2011.Views.Patients.Controls.FirstLook
{
	/// <summary>
	/// Interaction logic for Example.xaml
	/// </summary>
	public partial class SchedulerExample : System.Windows.Controls.UserControl
	{
        private static bool isAllDay = false;
	    /// <summary>
		/// Initializes a new instance of the <see cref="Example"/> class.
		/// </summary>
        public SchedulerExample()
		{
            LocalizationManager.DefaultCulture = new CultureInfo("es");
            LocalizationManager.Manager = new MedLink2011.Views.Patients.Controls.FirstLook.CustomLocalizationManager();
   
			InitializeComponent();
            scheduler.ResourceTypes[0].Resources.AddRange(new SpeakersList());
            scheduler.AppointmentsSource = new SessionAppointmentCollection(scheduler);
		}

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            RadSchedulerCommands.SaveAppointment.Execute(scheduler.SelectedAppointment, sender as Button);
        }

        private void EditRecurrenceRuleBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            RadSchedulerCommands.EditRecurrenceRule.Execute(scheduler.SelectedAppointment, sender as Button);
        }

        private void EditParentAppointmentBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            RadSchedulerCommands.EditParentAppointment.Execute(scheduler.SelectedAppointment, sender as Button);
        }

        private void RadContextMenu_ItemClick(object sender, RadRoutedEventArgs e)
        {
            RadMenuItem item = e.OriginalSource as RadMenuItem;

            AppointmentItem appItem = (sender as RadContextMenu).GetClickedElement<AppointmentItem>();
           
            if (appItem != null)
            {
                scheduler.SelectedAppointment = appItem.AppointmentSlot.Occurrence.Appointment;
            }

            string headerText = item.Header as string;
            
            switch (headerText)
            {
                case "Abrir":
                    Occurrence o = scheduler.SelectedAppointment.GetOccurrences(scheduler.SelectedAppointment.Start, scheduler.SelectedAppointment.End)[0] as Occurrence;
         
                    RadSchedulerCommands.EditAppointment.Execute(o, scheduler);
                    break;
                case "Eliminar":
                    RadSchedulerCommands.DeleteAppointment.Execute(scheduler.SelectedAppointment, scheduler);
                    break;
                case "Nueva Cita":
                    RadSchedulerCommands.CreateAppointment.Execute(null, scheduler);
                    break;
                case "Nuevo Evento de todo el dia":                   
                    isAllDay = true;
                    RadSchedulerCommands.CreateAppointment.Execute(null, scheduler);
                    break;
            }
        }

        private void scheduler_AppointmentCreating(object sender, AppointmentCreatingEventArgs e)
        {
            if (isAllDay)
            {
                SessionAppointment session = new SessionAppointment();
                session.IsAllDayEvent = true;
                isAllDay = false;
                e.NewAppointment = session;
            }
        }
	}
}